package com.jp.knowledge.logic;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.jiayen.util.ToasUtil;
import com.jp.knowledge.callback.JpCallBack;
import com.jp.knowledge.model.UserData;
import com.jp.knowledge.service.WebService;
import com.jp.knowledge.util.L;
import com.jp.knowledge.webUtil.X5WebView;

/**
 * Created by jiayen on 16/11/28.
 * <p>
 * 针对webService  逻辑处理
 */

public class WebServiceLogic implements ServiceConnection {
    private Context mContext;
    private WebService webService;

    private WebLogicCallBack callBack;
    private X5WebView serviceWebView;




    /**
     * 获取webServiceLogic
     *
     * @param context
     */
    public WebServiceLogic(Context context) {
        this.mContext = context;
        bindService();
    }

    public void setCallBack(WebLogicCallBack callBack) {
        this.callBack = callBack;
    }

    /**
     * 获取webView
     *
     * @return
     */
    public X5WebView getWebView() {
        return webService.getWebView();
    }


    /**
     * 设置webview的路径
     *
     * @param webView
     * @param path
     */
    public void setCurPath(X5WebView webView, String path) {
        if (webView != null)
            webView.loadUrl("javascript:windowNativeBridge.setCurrentPath('" + path + "')");
        else
            ToasUtil.toast(mContext, "对象为空！");
    }


    /**
     * 设置回调
     *
     * @param callback
     */
    public void setCallback(JpCallBack callback) {
        webService.setCallBack(callback);
    }


    /**
     * 设置webview的路径
     *
     * @param path
     * @param path
     */
    public void setCurrentPath(String path) {
//        if(serviceWebView!=null)
//            serviceWebView.loadUrl("javascript:windowNativeBridge.setCurrentPath('"+path+"')");
//        else
//            ToasUtil.toast(context,"对象为空！");
        if (webService != null)
            webService.setCurrentPath(path);
    }

    public String getCurrentPath(){
        if(webService != null)
            return webService.getCurrentPath();
        else
            return null;
    }

    /**
     * 绑定webView
     */
    private void bindService() {
        L.e("WebServiceLogic", "========bindService===============");
        mContext.bindService(new Intent(mContext, WebService.class), this, Context.BIND_DEBUG_UNBIND);

    }

    public void setUserData(UserData userData) {
        if(webService!=null)
        webService.setUserData(userData);
        else
            mContext.startService(new Intent(mContext,WebService.class));
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        L.e("WebServiceLogic", "========onServiceConnected===============");
        webService = ((WebService.LocalBinder) service).getService();
        serviceWebView = webService.getWebView();
        if (callBack != null&&webService!=null) callBack.done(serviceWebView);
        if(webService==null)ToasUtil.toast(mContext,"service都停止了，你还加载个毛线.....");
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }

    public interface WebLogicCallBack {
        void done(X5WebView webView);
    }

    /**
     * 解除绑定
     */
    public void unBind() {
        mContext.unbindService(this);
    }
}
